package com.example.springboot.mapper;

import com.example.springboot.entity.Student;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface StudentMapper {

    @Select("SELECT * FROM student")
    List<Student> findAllStudent();

    @Select("SELECT Email FROM student")
    List<Student> findAllEmail();

    @Select("SELECT StudentID, Avatar, PhoneNumber, Email, Nickname, Gender, IdNumber FROM student WHERE Email = #{email}")
    Student findUserInfoByEmail(@Param("email") String email);

    @Select("SELECT * FROM student WHERE Email = #{email} OR PhoneNumber = #{phoneNumber}")
    Student findStudent(@Param("email") String email, @Param("phoneNumber") String phoneNumber);

    @Insert("INSERT INTO student (StudentID, Avatar, PhoneNumber, Email, Password, Nickname, Gender, IdNumber) " +
            "VALUES (#{studentID}, #{avatar}, #{phoneNumber}, #{email}, #{password}, #{nickname}, #{gender}, #{idNumber})")
    void register(Student student);

    @Select("SELECT * FROM student WHERE Email = #{email}")
    Student findStudentByEmail(@Param("email") String email);

    @Select("SELECT * FROM student WHERE PhoneNumber = #{phoneNumber}")
    Student findStudentByPhone(@Param("phoneNumber") String phoneNumber);

    @Update("UPDATE student SET Email = #{newEmail} WHERE Email = #{oldEmail}")
    void updateEmail(@Param("oldEmail") String oldEmail, @Param("newEmail") String newEmail);

    @Update("UPDATE student SET PhoneNumber = #{newPhone} WHERE PhoneNumber = #{oldPhone}")
    void updatePhone(@Param("oldPhone") String oldPhone, @Param("newPhone") String newPhone);

    @Update("UPDATE student SET Password = #{newPassword} WHERE Email = #{email}")
    void updatePassword(@Param("email") String email, @Param("newPassword") String newPassword);
}
